package com.honey.solr.model;

import java.util.List;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.Clazz;
import com.honey.compilation.java.EnumField;
import com.honey.compilation.java.Field;
import com.honey.compilation.java.InnerClass;
import com.honey.compilation.java.InnerEnum;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.Parameter;
import com.honey.compilation.java.StaticBlock;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.ContentType;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.generator.java.extension.point.GeneratorJavaClass;
import com.honey.solr.SchemaField;
import com.honey.solr.SolrFactory;

public class SolrModel implements  GeneratorJavaClass{

	private SolrFactory solrFactory = null;
	
	private Clazz solrModel = null;
	
	public SolrModel(SolrFactory solrFactory){
		this.solrFactory = solrFactory;
	}
	
	@Override
	public AbstractJavaCompilation createNewCompilation(
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {		
		try {
			solrModel = new Clazz(solrFactory.getModelType());
			solrModel.setSuperClass(solrFactory.getModelSuperClassType());
			
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return solrModel;
	}
	
	
	@Override
	public FullyQualifiedJavaType[] importImportClass() {
		return null;
	}

	@Override
	public Method[] importMethods() {
		return null;
	}

	@Override
	public Field[] importFields() {
		return null;
	}

	@Override
	public StaticBlock[] importStaticBlocks() {
		return null;
	}

	@Override
	public InnerClass[] importInnerClass() {
		return null;
	}

	@Override
	public InnerEnum[] importInnerEnumClass() {
		InnerEnum[] amswer = new InnerEnum[1];
		try {
			InnerEnum  inner = new InnerEnum( solrFactory.getModelTableType() );
			List<SchemaField>  list =  solrFactory.getSchemaFields();
			for(SchemaField sf : list ){
				Parameter par =new Parameter( "String",  sf.getName() );
				EnumField ef = new EnumField( sf.getName().toUpperCase(), sf.getComment() ); 
				ef.addParameter(par, sf.getName() ) ;
				inner.addEnumFields(ef);
			}
			Field field = new Field( "value",JDKFullyQualifiedJavaType.getStringInstance());
			field.setUseDefaultValue(false);
			field.getDecoration().setFinal(true);
			inner.addField(field);
			Method method = new Method(inner.getType().getShortName());
			method.getDecoration().setConstructor(true);
			method.addParameter( new Parameter( "String",  "value") );
			method.addBodyLine("this.value = value");
			inner.addMethod(method);
			
			method = new Method("toString");
			method.addAnnotation("Override");
			method.setReturnType("String");
			method.addBodyLine("return this.value");
			inner.addMethod(method);
			
			amswer[0]=inner;
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		
		return amswer;
	}

	@Override
	public ContentType getContentType() {
		return null;
	}
	
	@Override
	public FullyQualifiedJavaType getType() {
		return solrModel.getType();
	}

}
